<?php

namespace App\Http\Controllers\Api\Train\Trip;

use App\Http\Controllers\Controller;
use App\Http\Requests\Train\Trip\InviteRequest;
use App\Http\Resources\Train\Trip\InviteResource;
use App\Models\Train\Invite\Invite;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class InviteController extends Controller
{
    private Invite $invite;
    private mixed  $user_id;

    public function __construct()
    {
        $this->invite  = new Invite();
        $this->user_id = auth()->id();
    }

    /**
     * @throws Exception
     */
    public function invite_info(InviteRequest $request)
    {
        $data                    = $request->all();
        $data['user_id']         = $this->user_id;
        $invite_info             = $this->invite->initInviteInfo($data);
        $invite_info->is_inviter = $invite_info->is_inviter($this->user_id);
        $invite_info->load('joins');
        $invite_info->expired = $invite_info->expired();
        $train_info           = $invite_info->train_info();
        Log::info(json_encode(['method' => '行程邀请', 'request' => $request->all(), 'response' => $invite_info], JSON_UNESCAPED_UNICODE));
        $invite_info = new InviteResource($invite_info);
        return $this->success('', compact('invite_info', 'train_info'));
    }

    /**
     * @throws Exception
     */
    public function invite_detail($invite_id)
    {
        $invite_info = $this->invite->getInviteInfo($invite_id);
        if (!$invite_info) {
            return $this->error('404', '邀请信息已过期');
        }
        Log::info(json_encode(['method' => '行程邀请详情', 'invite_id' => $invite_id], JSON_UNESCAPED_UNICODE));
        if ($invite_info->isExpired()) {
            return $this->error(404, '邀请信息已过期');
        }
        $invite_info->is_inviter = $invite_info->is_inviter($this->user_id);
        $train_info              = $invite_info->train_info();
        $invite_info->expired    = $invite_info->expired();
        $invite_info->load([
            'joins' => function ($query) use ($invite_info) {
                $query->when($invite_info->is_inviter == 0, function ($q) {
                    $q->where('user_id', $this->user_id);
                });
            },
        ]);

        !$invite_info->is_inviter && $invite_info->is_join = collect($invite_info->joins)->count() > 0 ? 1 : 0;
        $invite_info = new InviteResource($invite_info);

        return $this->success('', compact('invite_info', 'train_info'));
    }

    /**
     * @throws Exception
     */
    public function join($invite_id, Request $request)
    {
        $invite_info = $this->invite->getInviteInfo($invite_id);
        if (!$invite_info) {
            return $this->error('404', '邀请信息已过期');
        }

        if ($invite_info->isExpired()) {
            return $this->error('404', '邀请信息已过期');
        }

        $invite_info->is_inviter = $invite_info->is_inviter($this->user_id);
        if ($invite_info->is_inviter == 1) {
            return $this->error(1, '自己无法加入');
        }
        $invite_info->load([
            'joins' => function ($query) {
                $query->where('user_id', $this->user_id);
            },
        ]);

        if (collect($invite_info->joins)->count() == 0) {
            $join_data            = $request->all();
            $join_data['user_id'] = $this->user_id;
            $this->invite->participate($invite_info, $join_data);
        }
        !$invite_info->is_inviter && $invite_info->is_join = collect($invite_info->joins)->count() > 0 ? 1 : 0;
        $train_info  = $invite_info->train_info();
        $invite_info = new InviteResource($invite_info);
        return $this->success('', compact('invite_info', 'train_info'));
    }
}
